package gbench.tartarus.hipponoe;

import gbench.common.matlib.MatlibCanvas;
import gbench.tartarus.hipponoe.finance.acct.AccountingApp;
import gbench.tartarus.hipponoe.stomp.AcctController;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * -Dserver.port=8082 -Dserver.servlet.context-path=/mall
 */
@ComponentScan(basePackages = {"gbench.tartarus.hipponoe.mvc",
        "gbench.tartarus.hipponoe.appitf", "gbench.tartarus.hipponoe.stomp"})
@SpringBootApplication
public class HipponoeBootstrap extends SpringBootServletInitializer
        implements ApplicationContextAware {

    @Bean
    ApplicationRunner onStarted() {
        return args -> {
            pool.submit(AccountingApp::trialBalance);
            acctController.start("/topic/acct_balance");
            MatlibCanvas.print("HipponoeBootstrap",context,port);
        };
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        appContext = applicationContext;
    }

    @Autowired
    protected AcctController acctController;
    protected final ExecutorService pool = Executors.newFixedThreadPool(1); // 线程池
    protected ApplicationContext appContext;// 应用程序spring容器对象
    @Value("${server.port}")
    protected String port;
    @Value("${server.servlet.context-path}")
    protected String context;

    public static void main(String args[]) {
        SpringApplication.run(HipponoeBootstrap.class);
    }

}
